import {
  HTTP_GET,
} from "../../http/lib/index.js";
import GlobalData from '../../globalData'



export const uploadApi = {
  upload: (file) => {
    return new Promise((resolve, reject) => {
      const filesData = [];
      const token = wx.getStorageSync('token')

      if (wx.$_.isArray(file) && file.length) {
        let finishedCount = 0; // 计数已上传完成的文件数量

        for (let i of file) {
          wx.uploadFile({
            filePath: i?.tempFilePath || i?.path || i?.url || '',
            name: 'file',
            url: `${GlobalData.baseUrl}${GlobalData.requestUrlPath.replace('/','')}/system/file/upload`,
            header: {
              'Authorization': token
            },
            success(res) {
              const body = JSON.parse(res.data);
              const {
                code,
                msg,
                data
              } = body;

              if (code === 200) {
                filesData.push(...data, );
              } else {
                wx.showToast({
                  title: msg,
                  icon: 'error'
                });
                reject(msg);
                return;
              }

              finishedCount++;

              // 所有文件上传完成时，返回结果
              if (finishedCount === file.length) {
                resolve(filesData);
              }
            },
            fail(error) {
              wx.showToast({
                title: error.errMsg || 'Upload Failed',
                icon: 'error'
              });
              reject(error);
            }
          });
        }
      } else {
        resolve(filesData);
      }
    });
  }
}



// 获取部门小组用户联动数据
export const SystemApi = {
  getDeptTeamUser: (cb) => HTTP_GET('/sys/common/dept/team/user', '', cb),
  getDicList: (cb) => HTTP_GET('/system/dict/data/cache', '', cb),
  getDeptList: (cb) => HTTP_GET('/system/dept/cache', '', cb),
  getUserList: (cb) => HTTP_GET('/system/user/cache', '', cb),
  // 获取系统配置
  getSystemConfig: (cb, key) => HTTP_GET('/sys/', key, cb),
  // 获取登录用户信息
  getUserInfo: (cb) => HTTP_GET('/system/user/profile/getInfo', null, cb)


}